using System;
using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using CommunityServer.Components;
using CommunityServer.Blogs.Components;

namespace CS.Modules
{
    /// <summary>
    /// Auto-tweets a new blog post
    /// </summary>
    public class TweetModule : ICSModule
    {
        #region Constructor
        /// <summary>
        /// Default constructor
        /// </summary>
        public TweetModule()
        {
        }
        #endregion

        #region ICSModule Members
        /// <summary>
        /// Wire up events that will trigger user activity submission.
        /// </summary>
        /// <remarks>Note that the XML node is not currently used.
        /// All settings that this module uses are handled by the PseudoRule</remarks>
        /// <param name="csa">The application associated with this module</param>
        /// <param name="node">Additional configuration data</param>
        public void Init(CSApplication csa, XmlNode node)
        {
            // connect to after post updated event
            csa.PostPostUpdate += new CSPostEventHandler(AfterPostUpdated);
        }
        #endregion
        
        #region Event Handlers

        /// <summary>
        /// Handle event fired after post change is commited
        /// </summary>
        /// <param name="content">the content (i.e. - the blog post)</param>
        /// <param name="e">the event information</param>
        private void AfterPostUpdated(IContent content, CSPostEventArgs e)
        {
            // ignore event if module is disabled or invalid
            if (PostTweeter.Instance.Rule == null || !PostTweeter.Instance.Rule.EnableModule)
            {
                EventLogs.Info("Post Tweet Module is Disabled.", "Twitter", 986);
                return;
            }

            switch (e.ApplicationType)
            {
                // currently only handl weblog app type
                case ApplicationType.Weblog:
                    {
                        // get post being updated
                        WeblogPost post = content as WeblogPost;
                        // only tweeting posts of type Post
                        if (post != null && post.BlogPostType == BlogPostType.Post)
                        {
                            try
                            {
                                // only tweeting on post creation
                                switch (e.State)
                                {
                                    case ObjectState.Create:
                                        {
                                            // check if it is approved and not external
                                            if (post.IsApproved && PostTweeter.Instance.Rule != null)
                                            {
                                                // if external posts are not to be sent to twitter, then ignore them
                                                if (!PostTweeter.Instance.Rule.TweetExternal && post.IsExternal) { break; }

                                                // finally, attempt the tweet
                                                if (PostTweeter.Instance.TweetPost(post))
                                                {
                                                    EventLogs.Info(string.Format("Successful Post Tweet: {0}", post.Subject), "Twitter", 985);
                                                }
                                            }

                                            break;
                                        }
                                    default:
                                        break;
                                }
                            }
                            catch (Exception ex)
                            {
                                EventLogs.Write(
                                       string.Format("Exception in AfterPostUpdated for Tweet Module:{0}/n{1}", ex.Message, ex.StackTrace),
                                       "Twitter", 987, EventType.Error);
                            }
                        }
                        break;
                    }
                default:
                    break;
            }
        }

        #endregion

    }
}
